﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using QuanLyKhachSanDAO.DTO;

namespace QuanLyKhachSanDAO.DAO
{
    public class LoaiKhachDAO
    {
        protected object GetDataFromDataRow(DataTable dt, int i)
        {
            LoaiKhachDTO lk = new LoaiKhachDTO();
            lk.MaLoaiKhach = int.Parse(dt.Rows[i]["MaLoaiKhach"].ToString());
            lk.TenLoaiKhach = dt.Rows[i]["TenLoaiKhach"].ToString();
            lk.HeSo = float.Parse(dt.Rows[i]["HeSo"].ToString());
            lk.TinhTrang = bool.Parse(dt.Rows[i]["TinhTrang"].ToString());
            
            return (object)lk;
        }
        public  LoaiKhachDTO[] LayDanhSachLoaiKhach()
        {
            LoaiKhachDTO[] arr;
            string sql = "select * from LoaiKhach";
            DataProvider ds = new DataProvider();
            DataTable dt = ds.ExecuteQuery(sql);
            int i, n = dt.Rows.Count;
            arr = new LoaiKhachDTO[n];
            for (i = 0; i < n; i++)
            {
                object lk = GetDataFromDataRow(dt, i);
                arr[i] = (LoaiKhachDTO)lk;
            }
            return arr;
        }
        public DataTable LayHeSoTheoMaPhieuThue(int mapt)
        {
            DataTable tb = new DataTable();
            string sql = string.Format("select max(lk.heso)" +
            " from PhieuThuePhong pt,ChiTietPhieuThue ct, Khachhang kh, LoaiKhach lk" +
            " where pt.maphieuthuephong=ct.maphieuthue and ct.makhachhang=kh.makhachhang and kh.maloaikhach=lk.maloaikhach  and pt.maphieuthuephong=" + mapt);
            DataProvider ds = new DataProvider();
            tb = ds.ExecuteQuery(sql);
            return tb;
        }
        public  DataTable LayLoaiKhachTheoHeSo(float heso)
        {
            DataTable tb = new DataTable();
            string sql = string.Format("select TenLoaiKhach from LoaiKhach where Heso=" + heso);
            DataProvider ds = new DataProvider();
            tb = ds.ExecuteQuery(sql);
            return tb;
        }
    }
}